package com.woniuxy.credit_loans.staff_dong.service.impl;

import com.woniuxy.credit_loans.mapper.SysPermissionMapper;
import com.woniuxy.credit_loans.staff_dong.dto.PerDto;
import com.woniuxy.credit_loans.staff_dong.service.PermissionService;
import com.woniuxy.credit_loans.staff_dong.vo.PermissionVo;
import com.woniuxy.credit_loans.staff_dong.vo.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private SysPermissionMapper permissionMapper;
    @Override
    public List<PermissionVo> selMenuByAcc(String empAcc, Integer parentId) {
        List<PermissionVo> permissionVos = permissionMapper.selMenuByAcc(empAcc, parentId);
        if (permissionVos != null) {
            for (PermissionVo permissionVo : permissionVos) {
                List<PermissionVo> permissionVos1 = selMenuByAcc(empAcc, permissionVo.getPerId());
                permissionVo.setChildren(permissionVos1);
            }
        }
        return permissionVos;
    }

    @Override
    public List<TreeNode> selectAllPer(Integer parentId) {
        List<TreeNode> treeNodes = permissionMapper.selectAllPer(parentId);
        if (treeNodes != null) {
            for (TreeNode treeNode : treeNodes) {
                List<TreeNode> treeNodes1 = selectAllPer(treeNode.getId());
                treeNode.setChildren(treeNodes1);
            }
        }
        return treeNodes;
    }

    @Override
    public List<Integer> selectPerByRoleId(Integer roleId) {
        return permissionMapper.selectPerByRoleId(roleId);
    }

    @Override
    public int deleteByRoleId(Integer RoleId) {
        return 0;
    }

    @Override
    @Transactional
    public int insertPer(PerDto perDto) {
        if (perDto.getRoleId() != null) {
            int i = permissionMapper.deleteByRoleId(perDto.getRoleId());
        }
        int i = permissionMapper.insertPer(perDto);
        return 0;
    }
}
